class Foo(object):

    def doSomething(self):
        print('1111111')

    def close(self):
        print('close')

class context(object):
    def __enter__(self):
        self.data = Foo()
        return self.data

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.data.close()


with context() as f:
    print(f)
    # <__main__.Foo object at 0x000001EDE85B2F50>
    # close
